package org.codeminer.parts.server.actions;

/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import org.codeminer.database.DatabaseException;
import org.codeminer.database.DatabaseManager;
import org.codeminer.parts.constants.MessageConstants;
import org.codeminer.parts.database.PlayerConnector;
import org.codeminer.parts.devices.Device;
import org.codeminer.parts.devices.DeviceManager;
import org.codeminer.parts.player.Player;
import org.codeminer.parts.player.PlayerManager;
import org.codeminer.parts.server.permissions.Permission;

import java.util.EnumSet;
import java.util.Set;

public class DisconnectDevice implements Action {

    private final String VERSION = "3";

    private ActionContext context;

    private final Set<Permission> permissions = EnumSet.noneOf(Permission.class);

    private final Long playerId;
    private final Long deviceId;

    public DisconnectDevice(ActionContext context, Long playerId, Long deviceId) {
        this.context = context;

        this.playerId = playerId;
        this.deviceId = deviceId;

        this.permissions.add(Permission.MANAGE_PLAYER);
    }

    @Override
    public String getVersion() {
        return this.VERSION;
    }

    @Override
    public Set<Permission> requiredPermissions() {
        return this.permissions;
    }

    @Override
    public Response execute() {
        PlayerManager playerManager = context.getApplicationPart(PlayerManager.class);
        DeviceManager deviceManager = context.getApplicationPart(DeviceManager.class);
        DatabaseManager databaseManager = context.getApplicationPart(DatabaseManager.class);

        Player player = playerManager.getPlayer(getPlayerId());
        if (player == null)
            return createPlayerNotFound();
        Device device = deviceManager.getDevice(getDeviceId());
        if (device == null)
            return createDeviceNotFound();
        if (!player.removeDevice(device))
            return createNoConnection();

        try {
            PlayerConnector playerConnector = databaseManager.getConnector(PlayerConnector.class);
            playerConnector.update(player);
        } catch (DatabaseException e) {
            return createError(MessageConstants.ACTION_DEFAULT_ERROR, e.getMessage());
        }
        return createOK();
    }

    public Long getPlayerId() {
        return this.playerId;
    }

    public Long getDeviceId() {
        return this.deviceId;
    }

    private Response createOK() {
        Response response = new Response("OK");

        return response;
    }

    private Response createPlayerNotFound() {
        Response response = new Response("Error");
        response.addProperty("message.key", "InvalidId");
        response.addProperty("message", "Player with given id not found");
        return response;
    }

    private Response createDeviceNotFound() {
        Response response = new Response("Error");
        response.addProperty("message.key", "InvalidId");
        response.addProperty("message", "Device with given id not found");
        return response;
    }

    private Response createNoConnection() {
        Response response = new Response("Error");
        response.addProperty("message.key", "NoConnection");
        response.addProperty("message", "Device was never connected to the given player");
        return response;
    }

    private Response createError(String messageKey, String message) {
        Response response = new Response("Error");
        response.addProperty("message.key", messageKey);
        response.addProperty("message", message);
        return response;
    }

}
